﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContentRenderingApi;
using SmoLiteApi;
using ContentRenderingApi.Extender;
using System.Data;

namespace SmoLiteApi.Sql
{
    /// <summary/>
    internal class TableCreateSql : AbstractSql
    {
        /// <summary/>
        protected override string TemplateTypeName
        {
            get { return Constant.TEMPLATE_TYPE_NAME_TABLE_CREATE; }
        }

        /// <summary/>
        protected override void ConfigureTemplate(AbstractTemplate template)
        {
            // add conditionals
            template.AddConditionalFunction(Constant.ATTRIBUTE_LAST, value => value.Position == value.Length);

            // remove carriage returns and line feeds
            template.AssignDocumentOverride(TableCreateSql.RemoveLineFeeds);

            // add mappings for the sql data types
            template.AddWildcardOverride(
                Constant.ATTRIBUTE_DATA_TYPE,
                TableCreateSql.SqlDbType);
        }

        /// <summary/>
        private static void SqlDbType(NodeExtender nodeExtender, string name, List<object> values)
        {
            // fetch the data type
            SqlDbType sqlDbType = nodeExtender.FetchRawProperty<SqlDbType>(Constant.PROPERTY_SQL_DB_TYPE);

            // compare the data type name
            if (Enum.GetName(typeof(SqlDbType), sqlDbType) == name)
            {
                // add a flag
                values.Add(string.Empty);
            }
        }

        /// <summary/>
        private static string RemoveLineFeeds(string value)
        {
            return value.Replace(Constant.STRING_LINE_FEED_CARRIAGE_RETURN, string.Empty);
        }
    }
}
